<?php

class User_TouristComments{
    const MOD_TOURIST_TRACK = "mod_track_comments";
    const MOD_TOURIST_ACTIVITY = "mod_activity_comments";
    const MOD_TRACK = "mod_track";
    const MOD_ACTIVITY = "mod_activity";
    const GROUP = "default";

    //游客路线列表包含路线名和导游名
    public static function track_info($limit="",$offset="",$uid){
        list($list,$counts) = self::get_tourist_track_info($limit,$offset,$uid);
        $track_info = array();
        foreach($list as $v){
//            $track = self::track_info_check_out("id",$v['track_id'])->current();
//            $v['track_name'] = $track['name'];
            $guider = User_UserInfo::user_checkout('uid',$v['guider_id'])->current();
            $v['guider_icon'] = $guider['userIcon'];
            $v['guider_name'] = $guider['userFamily'].$guider['userName'];
            $track_info[] = $v;
        }
        return array($track_info,$counts);
    }

    //游客路线列表
    public static function get_tourist_track_info($limit,$offset,$uid){
        $query = $query_count = DB::select()->from(self::MOD_TOURIST_TRACK);
        if($uid != -1){
            $query = $query_count = $query->where('uid','=',$uid);
        }
//        echo $query;
        if($limit != "" || $offset != ""){
            $query = $query_count = $query->offset($offset)->limit($limit);
        }

        $count = $query_count->execute(self::GROUP)->count();
        $list = $query->execute(self::GROUP)->as_array();
        return array($list,$count);

    }

    //活动信息包含路线名和导游名
    public static function activity_info($limit="",$offset="",$uid){
        list($list,$counts) = self::get_tourist_activity_info($limit,$offset,$uid);
        $track_info = array();
        foreach($list as $v){
//            $track = self::activity_info_check_out("id",$v['activity_id'])->current();
//            $v['track_name'] = $track['name'];
            $guider = User_UserInfo::user_checkout('uid',$v['guider_id'])->current();
            $v['guider_icon'] = $guider['userIcon'];
            $v['guider_name'] = $guider['userFamily'].$guider['userName'];
            $track_info[] = $v;
        }
        return array($track_info,$counts);
    }

    //活动信息
    public static function get_tourist_activity_info($limit,$offset,$uid){
        $query = $query_count = DB::select()->from(self::MOD_TOURIST_ACTIVITY);
        if($uid != -1){
            $query = $query_count = $query->where('uid','=',$uid);
        }
//        echo $query;
        if($limit != "" || $offset != ""){
            $query = $query_count = $query->offset($offset)->limit($limit);
        }

        $count = $query_count->execute(self::GROUP)->count();
        $list = $query->execute(self::GROUP)->as_array();
        return array($list,$count);

    }

    //根据条件获取游客活动
    public static function tourist_activity_check_out($key,$val,$where=""){
        $query = DB::select()
            ->from(self::MOD_TOURIST_ACTIVITY)
            ->where($key,"=",$val);
        if(!empty($where)){
            $query = $query->where($where['key'],$where['cond'],$where['val']);
        }
        $query =$query->execute(self::GROUP);
        return $query;
    }

    //根据条件获取游客路线
    public static function tourist_track_check_out($key,$val,$where=""){
        $query = DB::select()
            ->from(self::MOD_TOURIST_TRACK)
            ->where($key,"=",$val);
        if(!empty($where)){
            $query = $query->where($where['key'],$where['cond'],$where['val']);
        }
         $query =$query->execute(self::GROUP);
        return $query;
    }
   //获得线路平均得分
    public static function tourist_track_score($key,$val,$where=""){
        $query = DB::select(array(DB::expr('AVG(track_score)'),'average'))
            ->from(self::MOD_TOURIST_TRACK)
            ->where($key,"=",$val);
        if(!empty($where)){
            $query = $query->where($where['key'],$where['cond'],$where['val']);
        }
        $query =$query->execute(self::GROUP);
        return $query->get('average');
    }
    //获得玩伴平均得分
    public static function tourist_guider_score($key,$val,$where=""){
        $query = DB::select(array(DB::expr('AVG(guider_score)'),'average'))
            ->from(self::MOD_TOURIST_TRACK)
            ->where($key,"=",$val);
        if(!empty($where)){
            $query = $query->where($where['key'],$where['cond'],$where['val']);
        }
        $query =$query->execute(self::GROUP);
        return $query->get('average');
    }

    public static function tourist_activity_score($key,$val,$where=""){
        $query = DB::select(array(DB::expr('AVG(track_score)'),'average'))
            ->from(self::MOD_TOURIST_TRACK)
            ->where($key,"=",$val);
        if(!empty($where)){
            $query = $query->where($where['key'],$where['cond'],$where['val']);
        }
        $query =$query->execute(self::GROUP);
        return $query->get('average');
    }

    //根据条件获取路线信息
    public static function track_info_check_out($key,$val){
        $query = DB::select()
            ->from(self::MOD_TRACK)
            ->where($key,"=",$val)
            ->execute(self::GROUP);
        return $query;
    }

    //根据条件获取活动信息
    public static function activity_info_check_out($key,$val){
        $query = DB::select()
            ->from(self::MOD_ACTIVITY)
            ->where($key,"=",$val)
            ->execute(self::GROUP);
        return $query;
    }

    //保存游客上传的文件
    public static function add_resource($file,$path){
        $files = "";
        foreach($file['name'] as $key =>$v){
            if($v == ""){
                continue;
            }
            $fileType = strstr($file['type'][$key],'image');
            if(!$fileType){
                $msg =  "不是图片文件";
                return array($msg,false);
            }
            if ($file["error"][$key] > 0){
                $msg =  "Return Code: " . $file["error"][$key] . "<br />";
                return array($msg,false);
            }
            else{
                $ext = explode('.',$v);
                $file_name = $path.md5($v.time()).".".$ext[count($ext)-1];
                move_uploaded_file($file["tmp_name"][$key],$file_name);
                $image = Image::factory($file_name);
                file_put_contents($file_name,$image->resize('200', '140', Image::NONE)->render());
                $files .= '/'.$file_name.';';
            }
        }

        return array($files,true);
    }

    //修改路线评价
    public static function tourist_track_update($data,$key,$value){
        $rows = DB::update(self::MOD_TOURIST_TRACK)
            ->set($data)
            ->where($key, '=', $value)
            ->execute(self::GROUP);
        return $rows;
    }

    //修改活动评价
    public static function tourist_activity_update($data,$key,$value){
        $rows = DB::update(self::MOD_TOURIST_ACTIVITY)
            ->set($data)
            ->where($key, '=', $value)
            ->execute(self::GROUP);
        return $rows;
    }

    public static function tourist_track_add($data){
        $rows = DB::insert(self::MOD_TOURIST_TRACK)
            ->columns (array_keys($data))
            ->values(array_values($data))
            ->execute(self::GROUP);
        return $rows;
    }

    public static function user_checkout($key,$val){
        $query = DB::select()
            ->from(self::MOD_TOURIST_TRACK)
            ->where($key,'=',$val)
            ->execute(self::GROUP);
        return $query;
    }
    //取得一条线路（活动、路线和自营）的评论数据
    public static function comments_check($id,$type,$where){
        return DB::select()
            ->from('mod_track_comments')
            ->where('type','=',$type)
            ->where('track_id','=',$id)
            ->where($where['key'],$where['cond'],$where['val'])
            ->execute();
    }
    public static function track_comments_check($id,$where){
        return DB::select()
            ->from('mod_track_comments')
            ->where('type','=','TRACK')
            ->where('track_id','=',$id)
            ->where($where['key'],$where['cond'],$where['val'])
            ->execute();
    }

    public static function activity_comments_check($id,$where){
        return DB::select()
            ->from('mod_track_comments')
            ->where('track_id','=',$id)
            ->where('type','=','ACTIVITY')
            ->where($where['key'],$where['cond'],$where['val'])
            ->execute();
    }

    public static function get_track($user,$offset,$limit){

        $count = DB::select(array('count("id")', 'counts'))
            ->from('app_order')
            ->where('uid', '=', $user->getUserUid())
            ->where('status', 'in', array(99,7))
            ->execute()
            ->get('counts');

        $query = DB::select()
            ->from('app_order')
            ->where('uid', '=', $user->getUserUid())
            ->where('status', 'in', array(99,7))
            ->order_by('created', 'desc')
            ->offset($offset)
            ->limit($limit)
            ->execute();

        $orders = array();
        foreach ($query as $rs)
        {
            $guid   = $rs['shopId'];
            $uid    = $rs['uid'];

            $guider = User::getUserFromUid($guid);
            $rs['guider']    = $guider->getUserInfo();
            $rs['statusName'] = self::orderStatusName($rs['status']);
            $queries = self::comments_status($uid,$rs['orderNo']);
            $rs['comments_id'] = $queries->get('id');

            $user = Util_User::getUserInfo($uid);
            $rs['userDetail'] = $user;

            $orders[] = $rs;
        }

        return array($count,$orders);
    }

    public static function orderStatusName ($statusId)
    {
        switch ($statusId)
        {
            case 0:
                return '等待付款';
                break;

            // 已付款
            case 1:
                return '等待确认';
                break;

            case 2:
                return '玩伴拒绝';
                break;

            // 玩伴同意后
            case 3:
                return '行程中';
                break;

            case 99:
                return '待评价';
                break;
            
            //评价完成
            case 7:
                return '已完成';
                break;

            // 退款流程
           case 4:
                return '发起退款';
                break;

            case 5:
                return '退款中';
                break;

            case 6:
                return '已退款';
                break;

            // 作废流程
            case 101:
                return '已过期';
                break;

            case 102:
                return '已删除';
                break;
        }
    }


    public static function comments_status($uid,$oid){
        return DB::select()
            ->from('mod_track_comments')
            ->where('uid','=',$uid)
            ->where('oid','=',$oid)
            ->execute()
            ;
    }

    public static function updateOrderStatus($oid){
        Order::setCommentFinished($oid);
    }

    public static function update_score($data,$table,$key,$value){
        $rows = DB::update($table)
            ->set($data)
            ->where($key, '=', $value)
            ->execute(self::GROUP);
        return $rows;

    }

}
